package com.wgts.wgts.mvp.view.activity.advertisement;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerAdvertisementActivityComponent;
import com.wgts.wgts.dagger.module.StartPageModule;
import com.wgts.wgts.mvp.presenter.IStartPagePersenter;
import com.wgts.wgts.mvp.view.IStartPageView;
import com.wgts.wgts.mvp.view.activity.awardmore.AwardMoreActivity;
import com.wgts.wgts.mvp.view.activity.goodsinfo.JDGoodinfoActivity;
import com.wgts.wgts.mvp.view.activity.guessing.GuessingActivity;
import com.wgts.wgts.mvp.view.activity.main.MoHuMainActivity;
import com.wgts.wgts.mvp.view.activity.webactivity.WebActivity;
import com.wgts.wgts.netmodle.resp.AdvertisingEntity;
import com.wgts.wgts.tools.CountDownTime;
import com.wgts.wgts.tools.CountDownTimerListener;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AdvertisementActivity extends BaseActivity implements IStartPageView {
    @BindView(R.id.img_advertisement)
    ImageView imgAdvertisement;

    public static final String ADVERTISEMENT = "ADVERTISEMENT";

    public AdvertisingEntity advertisingEntity;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.linear_jump)
    LinearLayout linearJump;

    private CountDownTime countDownTime;

    private CountDownTimerListener countDownTimerListener;

    private int time = 5;

    public boolean onClick = false;




    @Inject
    IStartPagePersenter persenter;


    @Override
    public void init() {
        DaggerAdvertisementActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .startPageModule(new StartPageModule(this))
                .build()
                .inject(this);
        advertisingEntity = getIntent().getParcelableExtra(ADVERTISEMENT);
    }

    @Override
    public void onCreate(Bundle saveInstanceBundle) {
        super.onCreate(saveInstanceBundle);
        setContentView(R.layout.advertisement_activity);
        ButterKnife.bind(this);

        initView();
    }

    private void initView() {


        if (advertisingEntity.bigImg != null) {
            Glide
                    .with(this)
                    .load(advertisingEntity.bigImg)
                    .into(imgAdvertisement);
        } else {
            Glide
                    .with(this)
                    .load(advertisingEntity.normalImg)
                    .into(imgAdvertisement);
        }

        persenter.jumpTimeSecond();
    }

    @OnClick({R.id.img_advertisement, R.id.linear_jump})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_advertisement:
                clickImg();
                break;
            case R.id.linear_jump:
                Intent intent = new Intent(AdvertisementActivity.this, MoHuMainActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }

    private void clickImg(){

        onClick = true;

        Intent mainIntent = new Intent(this, MoHuMainActivity.class);
        startActivity(mainIntent);

        Intent intent = null;
        if(advertisingEntity.actionUrlType.equals("0")){
            intent = new Intent(this, WebActivity.class);
            intent.putExtra(WebActivity.ACTION_URL, advertisingEntity.actionUrl);
            startActivity(intent);
        }else{
            try {
                JSONObject jsonObject = new JSONObject(advertisingEntity.actionUrl);
                switch (jsonObject.getString("code")){
                    case "5":
                        intent = new Intent(this, JDGoodinfoActivity.class);
                        intent.putExtra(JDGoodinfoActivity.PRODUCT_ID, jsonObject.getString("param"));
                        intent.putExtra(JDGoodinfoActivity.JOIN_TYPE, JDGoodinfoActivity.NOMAL);
                        break;
                    case "7":
                        intent = new Intent(this, AwardMoreActivity.class);
                        break;
                    case "8":
                        intent = new Intent(this, GuessingActivity.class);
                        break;
                    case "9":
                        break;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        if(intent != null){
            startActivity(intent);
        }
        finish();
    }



    @Override
    public void jumpResult(boolean jumpResult) {

    }

    @Override
    public void jumpTime() {

        if(time >= 1){
            time --;
            tvTime.setText(time + "跳过");
        }else{
            Log.d("removetime", "removetimesuccess");
            persenter.removeTime();
            if(!onClick){
                Intent intent = new Intent(AdvertisementActivity.this, MoHuMainActivity.class);
                startActivity(intent);
                finish();
            }
        }

    }

    @Override
    public void advertising(AdvertisingEntity entity) {

    }

    @Override
    public void pingResult() {

    }

    @Override
    public void onError(int status, String msg) {

    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        persenter.removeTime();
        Log.d("onDestroyremovetime", "onDestroyremovetimesuccess");
    }
}
